package com.basics.framework.config;


import com.basics.common.utils.StringUtils;
import com.basics.common.xss.XssFilter;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.DispatcherType;
import java.util.Map;

@Configuration
public class FilterConfig {
    @Value("${xss.enabled}")
    private String enabled;
    @Value("${xss.excludes}")
    private String excludes;
    @Value("${xss.urlPatterns}")
    private String urlPatterns;

    public FilterConfig() {
    }

    @Bean
    public FilterRegistrationBean xssFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setDispatcherTypes(DispatcherType.REQUEST, new DispatcherType[0]);
        registration.setFilter(new XssFilter());
        registration.addUrlPatterns(StringUtils.split(this.urlPatterns, ","));
        registration.setName("xssFilter");
        registration.setOrder(2147483647);
        Map<String, String> initParameters = Maps.newHashMap();
        initParameters.put("excludes", this.excludes);
        initParameters.put("enabled", this.enabled);
        registration.setInitParameters(initParameters);
        return registration;
    }
}
